package lizz.zhengzhou.screentimer;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    static final String TAG = "lizz.AutoStarter";

    @Override
    public void onReceive(Context context, Intent srcintent) {
        Log.d(TAG,"Recieved:"+srcintent.getAction());
        if (srcintent.getAction().equals(ACTION))
        {
            DevicePolicyManager  policyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            ComponentName adminReceiver = new ComponentName(context, ScreenOffAdminReceiver.class);

            if (policyManager.isAdminActive(adminReceiver)) {
                Intent serviceIntent = new Intent(context, BackGroundService.class);
                context.startService(serviceIntent);
            }else{
                Log.d(TAG,"Recieved:BOOT_COMPLETED");
                Toast.makeText(context, "Booting......", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(context, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                context.startActivity(intent);
            }
        }
    }
}
